잠시만 기다려 주세요

     '이태원 150명 넘게 죽은 핼러윈 참사.. 오세훈 탄핵.. 윤석열 탄핵.... 행안부 장관 이상민은 사람이냐... 말만 하면 책임 회피..'
전체검색 :  
이번주 로또 및 연금번호 발생!!   |  HOME   |  여기는?   |  바다물때표   |  알림 (16)  |  여러가지 팁 (1054)  |  추천 및 재미 (150)  |  자료실 (22)  |  
시사, 이슈, 칼럼, 평론, 비평 (582)  |  끄적거림 (127)  |  문예 창작 (703)  |  바람 따라 (69)  |  시나리오 (760)  |  드라마 대본 (248)  |  
살인!


    postgresql

postgresql - PostgreSQL의 트리거(TRIGGER)와 함수(function) 예제
이 름 : 바다아이   |   조회수 : 8985         짧은 주소 : https://www.bada-ie.com/su/?591591789061

PostgreSQL의 트리거와 펑션 예제를 만들어 봤다.

자료를 찾는데 너무 힘들어서 나중에 잊기전에 정리해봤다.

 

 

 

● 우선 테스트용 테이블 작성 

 

CREATE TABLE datalog(

    logtime timestamp PRIMARY KEY,

    data varchar,

    content varchar

);

 

* 테이블 지우기

DROP TABLE datalog;

 

● 펑션(Function) 함수 예제

    이 함수가 실행되면 지정 테이블의 레코드가 10개를 넘으면 가장 오래된 것부터 지운다.

 

CREATE FUNCTION data_fun() RETURNS trigger AS $data_fun$

    DECLARE

rowcount integer;

        delcount integer;

        maxcount integer;

    BEGIN

        maxcount := 10;

        SELECT count(*) logtime into rowcount  FROM datalog;

        delcount := rowcount - maxcount + 1;

 

        IF delcount > 0 THEN

   DELETE FROM datalog WHERE logtime IN (SELECT logtime FROM datalog ORDER BY logtime LIMIT delcount);

        END IF;

 

RETURN NEW;

    END;

$data_fun$ LANGUAGE plpgsql;

 

* 펑션 지우기

DROP FUNCTION data_fun();

 

● 트리거(Trigger) 함수 예제

    지정 Table에서 추가나 변경이 이루어질 때 지정 펑션이 실행된다.

 

CREATE TRIGGER data_trg BEFORE INSERT OR UPDATE ON datalog

    FOR EACH ROW EXECUTE PROCEDURE data_fun();

 

* 트리거 지우기

DROP TRIGGER data_trg ON datalog;

 

 

● 테스트용 데이터

 

insert into datalog(logtime, data) values('2015-01-01 12:12:01', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:02', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:03', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:04', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:05', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:06', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:07', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:08', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:12:09', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:01', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:02', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:03', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:04', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:05', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:06', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:07', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:08', 'aa');

insert into datalog(logtime, data) values('2015-01-01 12:13:09', 'aa');

 
위의 데이터를 입력하면 로그는 10개 행만 테이블에 저장된다.
데이터를 추가하기 전에 트리거의 연결된 펑션이 실행되어
테이블의 행이 10개를 넘어가면 가장 옛날 데이터를 삭제한다.


출처 : http://wwwi.tistory.com/348
 
| |





      1 page / 2 page
번 호 카테고리 제 목 이름 조회수
45 postgresql postgresql ... postgresql 14 .. postgresql.conf port 5432 .. 바다아이 678
44 postgresql , count(*) .... 바다아이 6857
43 postgresql How to do an update + join in PostgreSQL?, 바다아이 6088
42 postgresql sequence(퀀) 바다아이 7720
41 postgresql , , , index create, , 바다아이 8101
40 postgresql postgresql log_timezone .... 바다아이 6505
39 postgresql postgresql SEQUENCE reset .... 바다아이 8340
38 postgresql [PostgreSql] WITH , , Operator 바다아이 7732
37 postgresql postgresql for, foreach , 바다아이 9290
36 postgresql postgresql , , into ... 바다아이 9285
35 postgresql postgresql PL/pgSQL - SQL Procedural Language, , 바다아이 10701
34 postgresql postgresql ... .. , , 바다아이 11714
33 postgresql postgresql CSV export/import 바다아이 8518
32 postgresql postgresql tablespace , .... 바다아이 12840
31 postgresql postgresql 10 partitioning, ... , ... 바다아이 9259
30 postgresql Using PostgreSQL Arrays, ... ... 바다아이 9224
현재글 postgresql PostgreSQL (TRIGGER) (function) 바다아이 8986
28 postgresql Optimize and Improve PostgreSQL Performance with VACUUM, ANALYZE, and REINDEX 바다아이 9633
27 postgresql postgresql tuple . vacuumdb .. , . 바다아이 9033
26 postgresql postgresql , .. 바다아이 9230
25 postgresql postgresql , size, 바다아이 11226
24 postgresql postgresql , , .... 바다아이 8274
23 postgresql PostgreSQL Replication, , , master, slave 바다아이 10772
22 postgresql postgresql case 바다아이 8139
21 postgresql postgresql with 바다아이 8578
20 postgresql postgresql , , string 바다아이 11484
19 postgresql Postgresql partitioning table , , , 바다아이 9075
18 postgresql PostgreSQL 바다아이 10566
17 postgresql postgresql vacuumdb, psql, pg_dump password crontab , pgpass 바다아이 10249
16 postgresql postgresql sequence 퀀 auto_increment . 바다아이 9670
| |









Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천  실행시간 : 0.05793
to webmaster... gogo sea. gogo sea.